package com.love_weinuo.ui.fragment;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.love_weinuo.R;
import com.love_weinuo.application.LoveApplication;
import com.love_weinuo.ui.activity.LoginActivity;
import com.love_weinuo.util.dialog.InCommonDialog;
import com.love_weinuo.util.listener.OnBHClickListener;

/**
 * Created by Administrator
 * on 2019/2/21.
 */


public abstract class BaseLazyFragment extends BaseFragment{

    public static final String TAG = BaseLazyFragment.class.getSimpleName();
    public boolean isFragmentVisiable,isFirstVisible,ReuseView;
    private View rootView;

    protected View contentView;
    private ProgressDialog mProgressDialog;
    protected boolean isVisible;
    protected boolean isInit = false;
    protected boolean isLoad = false;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (contentView != null && contentView.getParent() != null) {
            ((ViewGroup) contentView.getParent()).removeView(contentView);
        }
        if (contentView == null) {
            contentView = inflaterView(inflater, container, savedInstanceState);
            if (contentView.getParent() != null) {
                ((ViewGroup) contentView.getParent()).removeView(contentView);
            }
        }
        isInit = true;
        isCanLoadData();
        return contentView;
    }

    /**
     * 初始化界面
     */
    @SuppressLint("InflateParams")
    protected abstract View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

    /**
     * 是否可以加载数据
     * 可以加载数据的条件：
     * 1.视图已经初始化
     * 2.视图对用户可见
     */
    private void isCanLoadData() {
        if (!isInit) {
            return;
        }
        if (getUserVisibleHint()) {
            lazyLoad();
            isLoad = true;
        } else {
            if (isLoad) {
                onVisible();
            }
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isCanLoadData();
    }

    /**
     * 可见
     */
    protected abstract void onVisible();

    /**
     * 不可见
     */
    protected void onInvisible() {
        cancelProgressDialog();
    }

    /**
     * 延迟加载
     * 子类必须重写此方法
     */
    protected abstract void lazyLoad();

    /**
     * 视图销毁的时候讲Fragment是否初始化的状态变为false
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isInit = false;
        isLoad = false;
    }

    /**
     * 取消加载dialog
     */
    @Override
    public void cancelProgressDialog(){
        if(mProgressDialog != null){
            mProgressDialog.cancel();
        }
    }
    /**
     * 显示加载dialog
     */
    @Override
    public void showProgressDialog(String msg){
        if(mProgressDialog == null){
            mProgressDialog = new ProgressDialog(getActivity());
            mProgressDialog.setCancelable(true);
        }
        //如果显示的字符为空，则设置默认值
        if(TextUtils.isEmpty(msg)){
            msg = "正在加载数据...";
        }
        mProgressDialog.setMessage(msg);
        mProgressDialog.show();
    }

    public void setTitleText(String str) {
        TextView text = getActivity().findViewById(R.id.bar_title);
        text.setText(str);
    }

    public void setRightText(String str) {
        TextView text = getActivity().findViewById(R.id.rightText);
        text.setText(str);
        text.setOnClickListener(this);
    }

    public void setRightImg(int id) {
        ImageView image = getActivity().findViewById(R.id.rightImage);
        image.setImageResource(id);
        image.setOnClickListener(this);
    }

    public void setLeftText(String str) {
        getActivity().findViewById(R.id.topBarRelative).setVisibility(View.VISIBLE);
        getActivity().findViewById(R.id.left_icon).setVisibility(View.GONE);
        TextView text = getActivity().findViewById(R.id.leftTitleText);
        text.setText(str);
        text.setOnClickListener(this);
    }

    public void showInCommonDialog(String string) {
        InCommonDialog dialog = new InCommonDialog(getActivity());
        dialog.setTitleText(string);
        dialog.show();
    }

    public void showInCommonDialog() {
        InCommonDialog dialog = new InCommonDialog(getActivity());
        dialog.setTitleText("未登录，请先登录");
        dialog.show();
        dialog.setOnBHClickListener(new OnBHClickListener() {
            @Override
            public void onConfirmClick() {
                LoveApplication.getInstance().startActivity(new Intent(LoveApplication.getInstance(), LoginActivity.class));
            }

            @Override
            public void onCloseClick() {

            }
        });
    }

}
